package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjNetworkEmpLevelRelation;
import com.zb.entity.ZjNetworkEmpSkill;
import com.zb.entity.ZjNetworkEmpSkillRelation;
import com.zb.extend.NetworkEmpSkillRelationExtend;
import com.zb.mapper.ZjNetworkEmpSkillRelationMapper;
import com.zb.service.ZjNetworkEmpSkillRelationService;
import com.zb.service.ZjNetworkEmpSkillService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Queue;

/**
 * <p>
 * 网点人员技能关联表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-26
 */
@Service
public class ZjNetworkEmpSkillRelationServiceImpl extends ServiceImpl<ZjNetworkEmpSkillRelationMapper, ZjNetworkEmpSkillRelation> implements ZjNetworkEmpSkillRelationService {
    @Resource
    private ZjNetworkEmpSkillService networkEmpSkillService;

    // 保存工程师技能
    @Override
    public void save(Long custAccId, Long networkEmpId, List<Long> networkEmpSkillIds) {
        deleteByCustAccIdAndNetworkEmpId(custAccId, networkEmpId);
        if (CollectionUtil.isEmpty(networkEmpSkillIds)) {
            return;
        }
        List<ZjNetworkEmpSkillRelation> adds = new ArrayList<>();
        ZjNetworkEmpSkillRelation empSkillRelation = null;
        for (Long networkEmpSkillId : networkEmpSkillIds) {
            empSkillRelation = new ZjNetworkEmpSkillRelation();
            empSkillRelation.setId(IdWorker.getId());
            empSkillRelation.setCustAccId(custAccId);
            empSkillRelation.setNetworkEmpId(networkEmpId);
            empSkillRelation.setNetworkEmpSkillId(networkEmpSkillId);
            adds.add(empSkillRelation);
        }
        this.saveBatch(adds);
    }

    // 根据商家ID & 工程师ID删除工程师技能
    @Override
    public void deleteByCustAccIdAndNetworkEmpId(Long custAccId, Long networkEmpId) {
        QueryWrapper<ZjNetworkEmpSkillRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId)
                .eq("network_emp_id", networkEmpId);
        this.remove(wrapper);
    }

    // 根据商家ID & 工程师ID获取工程师技能名称
    @Override
    public List<NetworkEmpSkillRelationExtend> getByNetworkIds(Long custAccId, List<Long> networkEmpIds) {
        if (CollectionUtil.isEmpty(networkEmpIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkEmpSkillRelation> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_emp_id", "network_emp_skill_id")
                .eq("cust_acc_id", custAccId)
                .in("network_emp_id", networkEmpIds);
        List<ZjNetworkEmpSkillRelation> empSkillRelations = this.list(wrapper);
        if (CollectionUtil.isEmpty(empSkillRelations)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkEmpSkill> empSkillQueryWrapper = new QueryWrapper<>();
        empSkillQueryWrapper.select("id", "name")
                .in("id", empSkillRelations.stream().map(ZjNetworkEmpSkillRelation::getNetworkEmpSkillId).toList());
        List<ZjNetworkEmpSkill> empSkillList = networkEmpSkillService.list(empSkillQueryWrapper);
        return empSkillRelations.stream().map(empSkill -> {
            NetworkEmpSkillRelationExtend extend = new NetworkEmpSkillRelationExtend();
            BeanUtil.copyProperties(empSkill, extend);
            empSkillList.stream().filter(f -> Objects.equals(f.getId(), empSkill.getNetworkEmpSkillId())).map(ZjNetworkEmpSkill::getName)
                    .findFirst().ifPresent(extend::setNetworkEmpSkill);
            return extend;
        }).toList();
    }

    // 根据商家ID & 工程师ID获取工程师技能ID
    @Override
    public List<Long> getNetworkEmpSkillIdsByNetworkEmpId(Long custAccId, Long networkEmpId) {
        QueryWrapper<ZjNetworkEmpSkillRelation> wrapper = new QueryWrapper<>();
        wrapper.select("network_emp_skill_id")
                .eq("cust_acc_id", custAccId).eq("network_emp_id", networkEmpId);
        return this.list(wrapper).stream().map(ZjNetworkEmpSkillRelation::getNetworkEmpSkillId).toList();
    }
}
